<?php
/**
 * Created by JetBrains PhpStorm.
 * User: heimuya
 * Date: 14-1-26
 * Time: 下午1:33
 * 主页控制器类
 */
class FoodAction extends AdminAction{
	
	public function _initialize(){
		parent::_initialize();
		$this->assign('page', __CLASS__);
	}
    /**
     * 
     */
    public function index(){
        $this->display();
    }
	
	/**
	 * 菜品信息
	 */
	 public function single(){
	 	$foodmodel = D("Food");
		$foodlist = $foodmodel->getFoodList();
		
		$categoryList = changeArrayKey(D("Category")->select());//获取加料
		
		$saucelist = changeArrayKey(D("Sauce")->select());//获取加料
		$tastelist = changeArrayKey(D("Taste")->select());//获取口味
		$extralist = changeArrayKey(D("Extra")->select());//获取扩展属性
		
		//解析json串
		foreach ($foodlist as $key => $value) {
			//加料 json串格式为{sid}
			$foodlist[$key]['sauce'] = jsonStringDecode($value['sauce'], $saucelist);
			
			//口味 json串格式为{tid}
			$foodlist[$key]['taste'] = jsonStringDecode($value['taste'], $tastelist);
			
			//扩展属性 json串格式为{eid}
			$foodlist[$key]['extra'] = jsonStringDecode($value['extra'], $extralist);
			
			//价格 json串格式为{id:price},若id为0，则表示标准的价格
			$price = json_decode($value['price'], TRUE);
			$foodlist[$key]['price'] = array();
			foreach ($price as $pkey => $pvalue) {
				if(isset($extralist[$pkey])){
					$foodlist[$key]['price'][$extralist[$pkey]['name']] = $pvalue;
				}else{
					$foodlist[$key]['price'][L('standard')] = $pvalue;
				}
			}
			
			//分类
			$categoryDetail = $categoryList[$foodlist[$key]['category']];
			if($categoryDetail['visible'] == 'v'){
				$foodlist[$key]['category'] = array(
					'cname'		=> $categoryDetail['name'],
					'cid'		=> $categoryDetail['id']
				);
			}
			
		}
		
		$this->assign('method', 'single');
		$this->assign('extralist', $extralist);
		$this->assign('categorylist', $categoryList);
		$this->assign('foodlist', $foodlist);
	 	$this->display();
	 }
	 
	/**
	 * 菜品信息
	 */
	 public function category(){
	 	$categoryList = D("Category")->select();//获取分类信息
	 	
		$this->assign('method', 'category');
	 	$this->assign('categorylist', $categoryList);
	 	$this->display();
	 }
	 
	/**
	 * 加料信息
	 */
	 public function sauce(){
	 	$sauceList = D("Sauce")->select();//获取分类信息
	 	
		$this->assign('method', 'sauce');
	 	$this->assign('saucelist', $sauceList);
	 	$this->display();
	 }
	 
	/**
	 * 口味信息
	 */
	 public function taste(){
	 	$tasteList = D("Taste")->select();//获取分类信息
	 	
		$this->assign('method', 'taste');
	 	$this->assign('tastelist', $tasteList);
	 	$this->display();
	 }
	 
	/**
	 * 扩展信息
	 */
	 public function extra(){
	 	$extraList = D("Extra")->select();//获取分类信息
	 	
		$this->assign('method', 'extra');
	 	$this->assign('extralist', $extraList);
	 	$this->display();
	 }
	 
	/**
	 * 商品属性信息
	 */
	 public function feature(){
	 	$foodmodel = D("Food");
		$foodlist = $foodmodel->getFoodList();
		
		$saucelist = changeArrayKey(D("Sauce")->select());//获取加料
		$tastelist = changeArrayKey(D("Taste")->select());//获取口味
		$extralist = changeArrayKey(D("Extra")->select());//获取扩展属性
		
		//解析json串
		foreach ($foodlist as $key => $value) {
			//加料 json串格式为{sid}
			$foodlist[$key]['sauce'] = jsonStringDecode($value['sauce'], $saucelist);
			
			//口味 json串格式为{tid}
			$foodlist[$key]['taste'] = jsonStringDecode($value['taste'], $tastelist);
			
			//扩展属性 json串格式为{eid}
			$foodlist[$key]['extra'] = jsonStringDecode($value['extra'], $extralist);
			
			//价格 json串格式为{id:price},若id为0，则表示标准的价格
			$price = json_decode($value['price'], TRUE);
			$foodlist[$key]['price'] = array();
			foreach ($foodlist[$key]['extra'] as $pkey => $pvalue) {
				if(isset($price[$pkey])){
					$foodlist[$key]['extra'][$pkey]['price'] = $price[$pkey];
				}else{
					$foodlist[$key]['extra'][$pkey]['price'] = '';
				}
				
			}
		}
		
		$this->assign('method', 'feature');
		$this->assign('saucelist', $saucelist);
		$this->assign('tastelist', $tastelist);
		$this->assign('extralist', $extralist);
		$this->assign('foodlist', $foodlist);
	 	$this->display();
	 }

	/**
	 * 商品属性编辑
	 */
	 public function featureDo(){
	 	$postData = $this->_post();
		dump($postData);
		$where['id'] = $postData['foodid'];
		foreach ($postData['sauceBelongs'] as $key => $value) {
			$postData['sauceBelongs'][$key] = intval($value);
		}
		foreach ($postData['tasteBelongs'] as $key => $value) {
			$postData['tasteBelongs'][$key] = intval($value);
		}
		$data['sauce'] = json_encode($postData['sauceBelongs']);
		$data['taste'] = json_encode($postData['tasteBelongs']);
		$extra = array();
		$price = array();
		foreach ($postData['price'] as $key => $value) {
			$extra[] = intval($key);
			if($value != ''){
				$price[$key] = floatval($value);
			}
		}
		$data['extra'] = json_encode($extra);
		$data['price'] = json_encode($price);
		
		D('Food')->where($where)->save($data);
		$this->systemAbstract();
		
		$this->redirect('Food/feature');
	 }
	 
	/**
	 * 新增信息
	 */
	 public function create(){
	 	//异步提交的数据
		$postdata = $this->_post();
		switch ($postdata['method']) {
			case 'single':
				$data['sequence'] = $this->checkSequence('Food', $postdata['foodsequence']);
				$data['name'] = $postdata['foodname'];
				$data['visible'] = $postdata['visible'];
				$data['category'] = $postdata['foodcategory'];
				D('Food')->add($data);
				break;
			case 'category':
				$data['sequence'] = $this->checkSequence('Category', $postdata['categorysequence']);
				$data['name'] = $postdata['categoryname'];
				$data['visible'] = $postdata['visible'];
				D('Category')->add($data);
				break;
			case 'sauce':
				$data['sequence'] = $this->checkSequence('Sauce', $postdata['saucesequence']);
				$data['name'] = $postdata['saucename'];
				$data['visible'] = $postdata['visible'];
				$data['price'] = $postdata['price'];
				D('Sauce')->add($data);
				break;
			case 'taste':
				$data['sequence'] = $this->checkSequence('Taste', $postdata['tastesequence']);
				$data['name'] = $postdata['tastename'];
				$data['visible'] = $postdata['visible'];
				D('Taste')->add($data);
				break;
			case 'extra':
				$data['name'] = $postdata['extraname'];
				$data['type'] = $postdata['type'];
				D('Extra')->add($data);
				break;
				
			default:
				
				break;
		}
		
		//if(isset($postdata['visible']) && $postdata['visible'] === 'v'){//若可见，则修改系统版本号
			$this->systemAbstract();
		//}
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
	 
	/**
	 * 编辑信息
	 */
	 public function edit(){
	 	//异步提交的数据
	 	$postdata = $this->_post();
		switch ($postdata['method']) {
			case 'single':
				$data['sequence'] = $this->checkSequence('Food', $postdata['foodsequence'], 'edit', $postdata['foodid']);
				$data['name'] = $postdata['foodname'];
				$data['visible'] = $postdata['visible'];
				$data['category'] = $postdata['foodcategory'];
				$where['id'] = $postdata['foodid'];
				D('Food')->where($where)->save($data);
				break;
			case 'category':
				$data['sequence'] = $this->checkSequence('Category', $postdata['categorysequence'], 'edit', $postdata['categoryid']);
				$data['name'] = $postdata['categoryname'];
				$data['visible'] = $postdata['visible'];
				$where['id'] = $postdata['categoryid'];
				D('Category')->where($where)->save($data);
				break;
			case 'sauce':
				$data['sequence'] = $this->checkSequence('Sauce', $postdata['saucesequence'], 'edit', $postdata['sauceid']);
				$data['name'] = $postdata['saucename'];
				$data['visible'] = $postdata['visible'];
				$data['price'] = $postdata['price'];
				$where['id'] = $postdata['sauceid'];
				D('Sauce')->where($where)->save($data);
				break;
			case 'taste':
				$data['sequence'] = $this->checkSequence('Taste', $postdata['tastesequence'], 'edit', $postdata['tasteid']);
				$data['name'] = $postdata['tastename'];
				$data['visible'] = $postdata['visible'];
				$where['id'] = $postdata['tasteid'];
				D('Taste')->where($where)->save($data);
				break;
			case 'extra':
				$data['name'] = $postdata['extraname'];
				$data['type'] = $postdata['type'];
				$where['id'] = $postdata['extraid'];
				D('Extra')->where($where)->save($data);
				break;
				
			default:
				
				break;
		}
		
		$this->systemAbstract();
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
	 
	/**
	 * 删除信息
	 */
	 public function delete(){
	 	//异步提交的数据
	 	$trans = array(
			'single'		=> 'Food',
			'category'		=> 'Category',
			'sauce'			=> 'Sauce',
			'taste'			=> 'Taste',
			'extra'			=> 'Extra'
		);
	 	$postdata = $this->_post();
		
		if($postdata['method'] !== 'extra'){
			$this->checkSequence($trans[$postdata['method']], 0, 'delete', $postdata['id']);
		}
		
		$where['id'] = $postdata['id'];
		D($trans[$postdata['method']])->where($where)->delete();
		
		$this->systemAbstract();
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
}